package cn.com.b2office.dao.hibernate;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.DeliveryDao;
import cn.com.b2office.domain.DeliveryFee;
import cn.com.b2office.domain.DeliveryMode;
import cn.com.b2office.domain.DeliveryTime;

public class DeliveryDaoHibernateImpl extends HibernateDaoSupport implements
		DeliveryDao {

	public int createDeliveryFee(DeliveryFee fee) {
		return (Integer) getHibernateTemplate().save(fee);
	}

	public int createDeliveryMode(DeliveryMode deliveryMode) {
		return (Integer) getHibernateTemplate().save(deliveryMode);

	}

	public int createDeliveryTime(DeliveryTime deliveryTime) {
		return (Integer) getHibernateTemplate().save(deliveryTime);
	}

	public void deleteDeliveryFee(DeliveryFee fee) {
		getHibernateTemplate().delete(fee);
	}

	public void deleteDeliveryMode(DeliveryMode deliveryMode) {
		getHibernateTemplate().delete(deliveryMode);
	}

	public void deleteDeliveryTime(DeliveryTime deliveryTime) {
		getHibernateTemplate().delete(deliveryTime);
	}

	@SuppressWarnings("unchecked")
	public List<DeliveryFee> findAllDeliveryFee() {
		Session s = getSession();
		try {
			Query query = s.createQuery("from DeliveryFee");
			query.setCacheable(true);
			return query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
	}

	@SuppressWarnings("unchecked")
	public List<DeliveryMode> findAllDeliveryMode() {
		Session s = getSession();
		try {
			Query query = s.createQuery("from DeliveryMode");
			query.setCacheable(true);
			return query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
	}

	@SuppressWarnings("unchecked")
	public List<DeliveryTime> findAllDeliveryTime() {
		Session s = getSession();
		try {
			Query query = s.createQuery("from DeliveryTime");
			query.setCacheable(true);
			return query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
	}

	// TODO:此处处理需要斟酌。(采用查询缓存还是直接数据库计算？)
	public DeliveryFee findDeliveryFee(int deliveryMode, double money) {
		List<DeliveryFee> list = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from DeliveryFee where mode=:mode")
					.setInteger("mode", deliveryMode);
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		if (list == null || list.isEmpty()) {
			DeliveryFee fee = new DeliveryFee();
			fee.setFee(0.0);
			fee.setMode(deliveryMode);
			return fee;
		} else {
			for (DeliveryFee fee : list) {
				if (fee.getMinValue() == money)
					return fee;
				if (fee.getMinValue() < money && fee.getMaxValue() > money)
					return fee;
			}
		}
		return null;
	}

	public Map<Integer, DeliveryFee> findDeliveryFee(double money) {
		// TODO Auto-generated method stub
		return null;
	}

	public DeliveryFee getDeliveryFee(int id) {
		return (DeliveryFee) getHibernateTemplate().get(DeliveryFee.class, id);
	}

	public DeliveryMode getDeliveryMode(int modeId) {
		return (DeliveryMode) getHibernateTemplate().get(DeliveryMode.class,
				modeId);
	}

	public DeliveryTime getDeliveryTime(int id) {
		return (DeliveryTime) getHibernateTemplate()
				.get(DeliveryTime.class, id);
	}

	public void updateDeliveryFee(DeliveryFee fee) {
		getHibernateTemplate().update(fee);
	}

	public void updateDeliveryMode(DeliveryMode deliveryMode) {
		getHibernateTemplate().update(deliveryMode);
	}

	public void updateDeliveryTime(DeliveryTime deliveryTime) {
		getHibernateTemplate().update(deliveryTime);
	}

	public DeliveryMode getDefaultDeliveryMode() {
		return findAllDeliveryMode().get(0);
	}

}
